package yijy.threadExample;

/**
 * Created by yijy on 2017/7/27.
 */
public class Syntest1 extends Thread{
    private int i=100;
    private boolean keeprunning=true;
    @Override
    public void run() {
        go();
    }

    public void go(){
        while(keeprunning){
            if(i>0){
                i--;
                System.out.println(Thread.currentThread().getName()+"取票成功，剩余"+i+"张票");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println(Thread.currentThread().getName()+"取票失败，票已售罄！");
                keeprunning=false;
            }
        }
    }

    public static void main(String[] args) {
        Syntest1 syntest1= new Syntest1();
        Thread t1 = new Thread(syntest1,"窗口1");
        Thread t2 = new Thread(syntest1,"窗口2");
        Thread t3 = new Thread(syntest1,"窗口3");
        Thread t4 = new Thread(syntest1,"窗口4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }


}

